﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            var name = textBox1.Text;
           
                var sql = string.Format("select * from Students where Name like '%{0}%'", name);
                var dt = DbHelper.GetDataTable(sql);
                dataGridView1.DataSource = dt;
            
           

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        //增
        private void button2_Click(object sender, EventArgs e)
        {
            Form2 form = new Form2();
            var gg = form.ShowDialog();
            if (gg == DialogResult.Yes)
            {
                var stuSql = "select * from Students";
                var dt = DbHelper.GetDataTable(stuSql);
                dataGridView1.DataSource = dt;
            } 
        }

        //改
        private void button3_Click(object sender, EventArgs e)
        {
            var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
            var name = (string)dataGridView1.SelectedRows[0].Cells["Name"].Value;
            var age = (int)dataGridView1.SelectedRows[0].Cells["Age"].Value;
            var score = (int)dataGridView1.SelectedRows[0].Cells["Score"].Value;
            Form2 form = new Form2(id, name, age, score);
            var gai = form.ShowDialog();
            if (gai == DialogResult.Yes)
            {
                var stuSql = "select * from Students";
                var dt = DbHelper.GetDataTable(stuSql);

                dataGridView1.DataSource = dt;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var stuSql = "select * from Students";
            var dt = DbHelper.GetDataTable(stuSql);
            dataGridView1.DataSource = dt;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        //删除
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];
                var cell = row.Cells[0];
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var sql = string.Format("delete from Students where Id={0}", id);
                var gg = DbHelper.data(sql);
                if (gg == 1)
                {
                    MessageBox.Show("删除成功", "提示");
                    var stuSql = "select * from Students";
                    var dt = DbHelper.GetDataTable(stuSql);
                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("删除失败，请重试", "提示");
                }
            }else {
                MessageBox.Show("请选择需要删除的数据","提示");
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
